package com.beemans.corekit.ui.view;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.beemans.corekit.R;
import com.beemans.corekit.integration.admob.IAbNativeSelfRender;
import com.google.android.gms.ads.MediaContent;
import com.google.android.gms.ads.nativead.MediaView;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdView;

public class CustomAbNativeRender implements IAbNativeSelfRender {
    @NonNull
    private final ViewGroup advertContainer;
    @NonNull
    private final NativeAdView selfRenderView;

    public CustomAbNativeRender(
            @NonNull ViewGroup advertContainer,
            @NonNull NativeAdView nativeAdView
    ) {
        this.advertContainer = advertContainer;
        this.selfRenderView = nativeAdView;
    }

    @Override
    public void onBindView(@NonNull NativeAd nativeAd, @NonNull View.OnClickListener closeListener) {
        selfRenderView.setVisibility(View.VISIBLE);

        final TextView tvFlFullClick = selfRenderView.findViewById(R.id.nativeSelf_tvFullClick);
        final TextView titleView = selfRenderView.findViewById(R.id.nativeSelf_tvAdTitle);
        final TextView descView = selfRenderView.findViewById(R.id.nativeSelf_tvAdDesc);
        final TextView ctaView = selfRenderView.findViewById(R.id.nativeSelf_tvInstall);
        final TextView adFromView = selfRenderView.findViewById(R.id.nativeSelf_tvAdFrom);
        final FrameLayout iconArea = selfRenderView.findViewById(R.id.nativeSelf_flAdImage);
        final MediaView mediaView = selfRenderView.findViewById(R.id.nativeSelf_flAdContentArea);
//        final ImageView logoView = selfRenderView.findViewById(R.id.nativeSelf_ivAdLogoPlatform);
        final ImageView ivClose = selfRenderView.findViewById(R.id.nativeSelf_ivClose);

        // close
        if (ivClose != null) {
            ivClose.setOnClickListener(closeListener);
        }

        // title
        selfRenderView.setHeadlineView(tvFlFullClick);

        final String headline = nativeAd.getHeadline();
        if (!TextUtils.isEmpty(headline)) {
            titleView.setText(headline);
            titleView.setVisibility(View.VISIBLE);
        }

        // body
        selfRenderView.setBodyView(descView);

        final String body = nativeAd.getBody();
        if (!TextUtils.isEmpty(body)) {
            descView.setText(body);
            descView.setVisibility(View.VISIBLE);
        }

        // cta
        selfRenderView.setCallToActionView(ctaView);

        final String callToAction = nativeAd.getCallToAction();
        if (!TextUtils.isEmpty(callToAction)) {
            ctaView.setText(callToAction);
            ctaView.setVisibility(View.VISIBLE);
        }

        // ad from
        selfRenderView.setAdvertiserView(adFromView);

        final String advertiser = nativeAd.getAdvertiser();
        if (!TextUtils.isEmpty(advertiser)) {
            adFromView.setText(advertiser);
            adFromView.setVisibility(View.VISIBLE);
        }

        // icon
        selfRenderView.setIconView(iconArea);

        final NativeAd.Image icon = nativeAd.getIcon();
        if (icon != null) {
            final ImageView iconView = new ImageView(selfRenderView.getContext());
            iconView.setImageDrawable(icon.getDrawable());
            iconArea.addView(iconView);
            iconArea.setVisibility(View.VISIBLE);
        }

        // media
        selfRenderView.setMediaView(mediaView);

        final MediaContent mediaContent = nativeAd.getMediaContent();
        mediaView.setMediaContent(mediaContent);

        // config complete
        selfRenderView.setNativeAd(nativeAd);
    }

    @NonNull
    @Override
    public NativeAdView getSelfRenderView() {
        return selfRenderView;
    }

    @Nullable
    @Override
    public ViewGroup getAdContainer() {
        return advertContainer;
    }
}